<?php

class JobsController extends \BaseController
{

    /**
     * Display a listing of the resource.
     * GET /jobs
     *
     * @return Response
     */
    public function index()
    {
        $jobs = Job::with(['category', 'resumes'])->paginate(15);
        $get_restore_id = Input::get('restore_id');
        if (!empty($get_restore_id)) {
            Job::withTrashed()->where('id', $get_restore_id)->restore();
            return Redirect::route('admin.jobs.index')->with('message', '撤销删除成功');
        }
        return View::make('jobs.index', compact('jobs'));
    }

    /**
     * Show the form for creating a new resource.
     * GET /jobs/create
     *
     * @return Response
     */
    public function create()
    {
        $categories = Category::get()->toArray();

        $jobs_category = array();

        foreach ($categories as $category) {
            $jobs_category = array_add($jobs_category, $category['id'], $category['name']);
        }

        return View::make('jobs.create', compact('jobs_category'));
    }

    /**
     * Store a newly created resource in storage.
     * POST /jobs
     *
     * @return Response
     */
    public function store()
    {
        $rules = array(
            'job_name' => 'required',
            'content' => 'required',
            'job_status' => 'required',
            'job_category' => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator);;
        }

        $job = new Job;

        $job->name = Input::get('job_name');
        $job->description = Input::get('content');
        $job->status = Input::get('job_status');
        $job->category_id = Input::get('job_category');
        $job->type = Input::get('job_type');
        $job->department = Input::get('job_department');
        $job->valid_data = Input::get('job_valid_date');
        $job->number = Input::get('job_number');
        $job->specialty = Input::get('job_specialty');
        $job->education = Input::get('job_education');
        $job->experience = Input::get('job_experience');
        $job->age = Input::get('job_age');
        $job->save();

        return Redirect::route('admin.jobs.edit', array('id' => $job->id))->with('message', '新职位添加成功！');

    }


    /**
     * Show the form for editing the specified resource.
     * GET /jobs/{id}/edit
     *
     * @param  int $id
     * @return Response
     */
    public function edit($id)
    {
        $categories = Category::get()->toArray();

        $jobs_category = array();

        foreach ($categories as $category) {
            $jobs_category = array_add($jobs_category, $category['id'], $category['name']);
        }

        $job = Job::find($id);

        if (!$job) {
            return Redirect::route('admin.jobs.index')->with('error', '你所要编辑的职位不存在');
        }

        return View::make('jobs.edit', compact('job', 'jobs_category'));
    }

    /**
     * Update the specified resource in storage.
     * PUT /jobs/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function update($id)
    {
        $rules = array(
            'job_name' => 'required',
            'content' => 'required',
            'job_status' => 'required',
            'job_category' => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator);;
        }

        $job = Job::find($id);

        $job->name = Input::get('job_name');
        $job->description = Input::get('content');
        $job->status = Input::get('job_status');
        $job->category_id = Input::get('job_category');
        $job->type = Input::get('job_type');
        $job->department = Input::get('job_department');
        $job->valid_data = Input::get('job_valid_date');
        $job->number = Input::get('job_number');
        $job->experience = Input::get('job_experience');
        $job->specialty = Input::get('job_specialty');
        $job->education = Input::get('job_education');

        $job->age = Input::get('job_age');
        $job->update();

        return Redirect::route('admin.jobs.edit', array('id' => $job->id))->with('message', '修改成功！');
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /jobs/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function destroy($id)
    {
        $job = Job::find($id);

        if (!$job) {
            return Redirect::route('admin.jobs.index')->with('error', '对不起，没找到相关的职位');
        }

        $job->delete();

        return Redirect::route('admin.jobs.index')->with('message', '删除成功。<a href="?restore_id=' . $job->id . '">点击这里撤销删除</a>。');
    }

}